//
//  CWHomeViewModel.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/26.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import YYWebImage
/**
 首页界面的viewmodel
 */
class CWHomeViewModel: NSObject {
    
    //懒加载数据源数组
    lazy var statusList: [CWStatusViewModel] = [CWStatusViewModel]()
    
    //加一个参数表示是上拉加载还是下拉刷新
    func loadData(isUp: Bool,callBack: @escaping (Bool)->()) {
        //上拉加载使用的maxid
        var maxID: Int64 = 0
        //下拉刷新使用的sinceID
        var sinceID: Int64 = 0
        if isUp {
            maxID = statusList.last?.statusModel?.id ?? 0
            if maxID > 0 {
                maxID -= 1
            }
        }else {
            sinceID = statusList.first?.statusModel?.id ?? 0
        }
        
        CWNetworkTool.sharedTools.requestStatuses(accessToken: (CWUserAccountViewModel.userAccountViewModel.userModel?.access_token)!,maxID: maxID,sinceID: sinceID) { (responseObject, error) in
            
            
            if error != nil {
                print(error!)
                callBack(false)
                return
            }
            let dict = responseObject as! [String: Any]
            
            let status = dict["statuses"] as! [[String: Any]]
            
            let modelArr = NSArray.yy_modelArray(with: CWStatuses.self, json: status) as! [CWStatuses]
            
            var muArr = [CWStatusViewModel]()
            //创建调度组
            let group = DispatchGroup()
            for model in modelArr {
                
                let tempModel = CWStatusViewModel()
                tempModel.statusModel = model
                muArr.append(tempModel)
                
                //判断是否有单张图片
                if model.pic_urls?.count == 1{
                    //判断原创微博单张图片
                    //进入调度组
                    group.enter()
                    YYWebImageManager.shared().requestImage(with: URL(string: (model.pic_urls?.first?.thumbnail_pic)!)!, options: [], progress: nil, transform: nil, completion: { (image, url, _, _, error) in
                        if error != nil{
                            print(error!)
                        }else{
                            
                        }
                        //离开调度组
                        group.leave()
                    })
                    
                }
                if model.retweeted_status?.pic_urls?.count == 1 {
                    //判断转发微博单张图片
                    //进入调度组
                    group.enter()
                    YYWebImageManager.shared().requestImage(with: URL(string: (model.retweeted_status?.pic_urls?.first?.thumbnail_pic)!)!, options: [], progress: nil, transform: nil, completion: { (image, url, _, _, error) in
                        if error != nil{
                            print(error!)
                        }else{
                            
                        }
                        //离开调度组
                        group.leave()
                    })
                }
            }
            
            //如果是上拉加载
            if isUp {
                self.statusList.append(contentsOf: muArr)
            }else {
                self.statusList.insert(contentsOf: muArr, at: 0)
            }
            
            //回到主线程
            group.notify(queue: DispatchQueue.main, execute: {
                //代码执行到此说明请求成功
                
                callBack(true)
            })
            
            
        }
        
    }

}
